package leetcode;

/**
 * @program: datastructureandalogorithm
 * @description:
 * @author: hmx
 * @create: 2021-12-05 18:42
 **/
public class LeetCode69 {

    //二分查找
    public int mySqrt(int x) {
        long low = 0;
        long high = x;
        long res = 0;
        while (low <= high) {
            long mid = low + high >> 1;
            if (mid * mid <= x) {
                res = mid;
                low = mid + 1;
            } else {
                high = mid - 1;
            }
        }
        return (int)res;
    }

    /*public int mySqrt(int x) {
        long low = 1;
        while (low * low <= x) {
            low++;
        }
        low--;
        return (int)low;
    }*/

}
